#include "mysql_chat_session.hpp"
#include "mysql_apply.hpp"
#include "mysql_relation.hpp"
#include <gflags/gflags.h>

DEFINE_bool(run_mode, false, "程序的运行模式, false-调试模式, true-发布模式");
DEFINE_string(log_file, "", "发布模式下, 用于指定日志文件的路径");
DEFINE_int32(log_level, 0, "发布模式下, 用于指定日志的输出等级");

void r_insert_test(suye::RelationTable& tb)
{
    tb.insert("用户ID1", "用户ID2");
    tb.insert("用户ID1", "用户ID3");
}

void r_select_test(suye::RelationTable& tb)
{
    auto res = tb.friends("用户ID1");
    for (const auto& uid: res) {
        std::cout << uid << std::endl;
    }
}

void r_remove_test(suye::RelationTable& tb)
{
    tb.remove("用户ID1", "用户ID3");
}

void r_exists_test(suye::RelationTable& tb) 
{
    std::cout << tb.exists("用户ID1", "用户ID2") << std::endl;
    std::cout << tb.exists("用户ID1", "用户ID3") << std::endl;
}

void a_insert_test(suye::FriendApplyTable& tb) 
{
    suye::FriendApply fa1("eid1", "用户ID1", "用户ID2");
    tb.insert(fa1);
    
    suye::FriendApply fa2("eid2", "用户ID1", "用户ID3");
    tb.insert(fa2);

    suye::FriendApply fa3("eid3", "用户ID2", "用户ID3");
    tb.insert(fa3);
}

void a_remove_test(suye::FriendApplyTable& tb) 
{
    tb.remove("用户ID2", "用户ID3");
}

void a_select_test(suye::FriendApplyTable& tb)
{
    auto res = tb.applyUsers("用户ID3");
    for (const auto& uid: res) {
        std::cout << uid << std::endl;
    }
}

void a_exists_test(suye::FriendApplyTable& tb) 
{
    std::cout << tb.exists("用户ID1", "用户ID2") << std::endl;
    std::cout << tb.exists("用户ID1", "用户ID3") << std::endl;
    std::cout << tb.exists("用户ID2", "用户ID3") << std::endl;
}

void c_insert_test(suye::ChatSessionTable& tb) 
{
    // suye::ChatSession cs1("会话ID1", "会话名称1", suye::ChatSessionType::SINGLE);
    // tb.insert(cs1);
    // suye::ChatSession cs2("会话ID2", "会话名称2", suye::ChatSessionType::GROUP);
    // tb.insert(cs2);
    suye::ChatSession cs3("会话ID3", "会话名称3", suye::ChatSessionType::SINGLE);
    tb.insert(cs3);
}

void c_select_test(suye::ChatSessionTable& tb) 
{
    auto res = tb.select("会话ID2");
    std::cout << res->chat_session_id() << std::endl;
    std::cout << res->chat_session_name() << std::endl;
    std::cout << (int)res->chat_session_type() << std::endl;
}

void c_single_test(suye::ChatSessionTable& tb) 
{
    auto res = tb.singleChatSession("用户ID2");
    for (const auto& info : res) {
        std::cout << info.chat_session_id << std::endl;
        std::cout << info.friend_id << std::endl;
    }
}
void c_group_test(suye::ChatSessionTable& tb) 
{
    auto res = tb.groupChatSession("用户ID2");
    for (const auto& info : res) {
        std::cout << info.chat_session_id << std::endl;
        std::cout << info.chat_session_name << std::endl;
    }
}

void c_remove_test(suye::ChatSessionTable& tb) 
{
    tb.remove("会话ID2");
    tb.remove("会话ID3");
}

void c_remove_test1(suye::ChatSessionTable& tb) 
{
    tb.remove("用户ID1", "用户ID2");
}

int main(int argc, char* argv[])
{
    google::ParseCommandLineFlags(&argc, &argv, true);
    suye::Init_logger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);
    
    auto db = suye::ODBFactory::create("root", "ok", "suye", "127.0.0.1", "utf8", 0, 1);
    suye::RelationTable rtb(db);
    suye::FriendApplyTable ftb(db);
    suye::ChatSessionTable ctb(db);

    // r_insert_test(rtb);
    // r_select_test(rtb);
    // r_remove_test(rtb);
    // r_exists_test(rtb);

    // a_insert_test(ftb);
    // a_remove_test(ftb);
    // a_select_test(ftb);
    // a_exists_test(ftb);

    // c_insert_test(ctb);
    // c_select_test(ctb);
    // c_single_test(ctb);
    // c_group_test(ctb);
    // c_remove_test(ctb);
    c_remove_test1(ctb);

    return 0;
}